﻿using Microsoft.AspNetCore.Mvc;

namespace Projects.OssDownload.Controllers;


//[ApiController]
[Route("dl")]
public class DownloadController : ControllerBase
{
    private readonly HttpClient _httpClient;

    public DownloadController(IHttpClientFactory httpClientFactory)
    {
        _httpClient = httpClientFactory.CreateClient();
    }

    [HttpGet("{*path}")]
    public async Task<IActionResult> Index(string path)
    {
        var remoteUrl = $"https://{path}";
        var response = await _httpClient.GetAsync(remoteUrl);

        if (!response.IsSuccessStatusCode)
        {
            return StatusCode((int)response.StatusCode);
        }

        var content = await response.Content.ReadAsByteArrayAsync();

        return File(content, "application/octet-stream", Path.GetFileName(path));
    }
}
